package com.freecat.xmqgct.app;

import android.content.Context;
import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import com.freecat.xmqgct.face.activity.FaceLivenessExpActivity;
import com.freecat.xmqgct.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;

import me.jessyan.art.http.GlobalHttpHandler;
import me.jessyan.art.utils.ArtUtils;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 作者：win
 * 时间：2019/9/16
 * 注释：
 *
 * @author ChenPeng
 */
public class GlobalHttpHandlerImp implements GlobalHttpHandler {
    private Context context;

    GlobalHttpHandlerImp(Context context) {
        this.context = context;
    }

    @Override
    public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
        if (!TextUtils.isEmpty(httpResult)) {
            try {
                JSONObject object = new JSONObject(httpResult);
//                String status = object.getString("status");
                System.out.println("object====================");
                String status = object.getString("code");

                System.out.println("object====================status"+status);
                if ("41".equals(status) || "42".equals(status)) {
                    ToastUtils.showShort("登录过期");
                    ArtUtils.startActivity(FaceLivenessExpActivity.class);
                    ActivityUtils.finishAllActivities();
                }

            } catch (Exception e) {
                e.printStackTrace();
                return response;
            }
        }
        return response;
    }

    @Override
    public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {

        String token = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.TOKEN);
//        String rybh = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.USER_ID);
        String appKey = "cq_gckczljgxt";
        String secret = "01c21da1-12a3-cbc7-be91-19b5b0ef21ab";
        String macAddress = DeviceUtils.getMacAddress();
        String ipAddress = NetworkUtils.getIPAddress(true);
        String imei = PhoneUtils.getIMEI();
        String timeMillis = String.valueOf(System.currentTimeMillis());
        JSONObject object = new JSONObject();
        try {
            object.put("KEY", appKey);
            object.put("SECRET", secret);
            object.put("TOKEN", token);
            object.put("MAC", macAddress);
            object.put("IP", ipAddress);
            object.put("IMEI", imei);
            object.put("TICKER", timeMillis);
            object.put("RANDOMKEY", UUID.randomUUID().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String s = null;
        try {
            String data = object.toString();
            byte[] bytes = EncryptUtils.encryptRSA(data.getBytes(), EncodeUtils.base64Decode(Constant.PUBLIC_KEY), true, "RSA/None/PKCS1Padding");
            s = EncodeUtils.base64Encode2String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chain.request().newBuilder()
//                .addHeader("appkey", appKey)
//                .addHeader("secret", secret)
//                .addHeader("token", token)
                .addHeader("Authorization", "Bearer " + s)
                .build();
    }
}
